﻿using ControlzEx.Theming;
using LiveCharts;
using Microsoft.Win32;
using SmartDevice.Library.Device.Power;
using SmartDevice.Library.Device.Voltmeter;
using SmartDevice.Models;
using Stylet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SmartDevice.Pages
{
    public class MainShellViewModel : Screen
    {
        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            InitDevice();
        }

        private readonly IPowerMaster powerMaster = new DCPowerMaster();
        public string VoltageInfo { get; set; } = "Connect...";

        private void InitDevice()
        {
            NetworkConfig networkConfig = new NetworkConfig
            {
                LocalIP = "127.0.0.1",
                LocalPort = 9000,
                DeviceIP = "127.0.0.1",
                DevicePort = 9001,
            };

            if (powerMaster.Connect(networkConfig))
            {
                powerMaster.StatusReport += PowerMaster_StatusReport;
            }
        }

        private void PowerMaster_StatusReport(PowerReportEventArgs args)
        {
            if (args.PowerStatus == PowerStatus.OFF)
            {
                VoltageInfo = "Power OFF";
            }

            if (args.PowerStatus == PowerStatus.ON)
            {
                VoltageInfo = $"DC {args.Voltage:000.0} V";
            }
        }

        public float Voltage { get; set; }

        public void PowerON()
        {
            powerMaster.SetPowerON(Voltage);
        }

        public void PowerOFF()
        {
            powerMaster.SetPowerOFF();
        }

    }
}
